'use strict';

const redisKey = require('../utils/redis_key');

module.exports = options => {
  return async function auth(ctx, next) {

    ctx.validate({
      token: 'string',
    }, ctx.request.headers);

    const redisKeyToken = redisKey.token(ctx.request.headers.token);
    const v = await ctx.app.redis.get(redisKeyToken);

    if (v) {
      ctx.v = JSON.parse(v);
      await next();
    } else {
      ctx.body = {
        code: ctx.app.config.code.NOT_AUTHORIZE,
        msg: 'not authorize',
      };
    }
  };
};
